package weiyao.xinxidasai.config.Redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.Random;

@Component
public class RedisCache {

    public static final String NULL_OBJECT = "null";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private BloomFilter bloomFilter; // 确保BloomFilter已注入

    // ================== 基础缓存操作 ==================
    public Object getCache(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void setCache(String key, Object value, long expireTime, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
        bloomFilter.add(key);
    }

    // ================== 分布式锁操作 ==================
    public boolean tryLock(String lockKey, long timeout, TimeUnit unit) {
        return redisTemplate.opsForValue().setIfAbsent(lockKey, "locked", timeout, unit);
    }

    public void unlock(String lockKey) {
        redisTemplate.delete(lockKey);
    }

    // ================== 高级缓存操作 ==================
    public void setCacheWithRandomExpire(String key, Object value, long minExpire, long maxExpire, TimeUnit unit) {
        if (minExpire < 0 || maxExpire < 0 || maxExpire <= minExpire) {
            throw new IllegalArgumentException("Invalid expire range");
        }

        long range = maxExpire - minExpire;
        long randomExpire = minExpire + new Random().nextInt((int) range);

        redisTemplate.opsForValue().set(key, value, randomExpire, unit);
        bloomFilter.add(key);
    }

    // ================== 其他实用方法 ==================
    public void deleteCache(String key) {
        redisTemplate.delete(key);
        bloomFilter.delete(key);
    }

    public boolean isLocked(String lockKey) {
        return redisTemplate.hasKey(lockKey);
    }
    public void deleteFromFilter(String key) {
        bloomFilter.delete(key);
    }
// ================== 哈希缓存操作 ==================

    public void setCacheMap(String key, Map<String, Object> entries,long minExpire, long maxExpire, TimeUnit unit) {
        redisTemplate.opsForHash().putAll(key, entries);
        long range = maxExpire - minExpire;
        redisTemplate.expire(key, range, unit);
        bloomFilter.add(key); // 添加key到布隆过滤器
    }


    public void setCacheMapValue(String key, Long field, Object value) {
        redisTemplate.opsForHash().put(key, field, value);
        bloomFilter.add(key);
    }

    public Map<Object, Object> getCacheMap(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public void setCacheMapValueWithRandomExpire(String key, Long field, Object value, long minExpire, long maxExpire, TimeUnit unit) {
        long randomExpire = minExpire + new Random().nextInt((int)(maxExpire - minExpire));
        redisTemplate.opsForHash().put(key, field, value);
        redisTemplate.expire(key, randomExpire, unit);
        bloomFilter.add(key);
    }
    public void deleteHashCache(String key, Object field) {
        // 强制转换为String类型
        String stringField = String.valueOf(field);
        redisTemplate.opsForHash().delete(key, stringField);
        // 保持键格式统一
        bloomFilter.delete(key + ":" + stringField);
    }


        public void setHashCache(String key, String field, String value) {
            redisTemplate.opsForHash().put(key, field, value);

            // 可选：设置过期时间（例如1小时）
            redisTemplate.expire(key, 1, TimeUnit.HOURS);
        }
    public <T> void increment(String key) {
        redisTemplate.opsForValue().increment(key);
    }

    public <T> Integer getCacheObject(String key) {
        return (Integer) redisTemplate.opsForValue().get(key);
    }
}